﻿using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Happy.Domain.EntityFramework.Test.Stub;

namespace Happy.Domain.EntityFramework.Test
{
    [TestClass]
    public class UnitOfWorkTest
    {
        private User _testUser = new User(Guid.NewGuid()) { Name = "段光伟", Age = 28 };

        [TestInitialize]
        public void TestInitialize()
        {
            using (var unitOfWork = new TestUnitOfWork())
            {
                var repository = unitOfWork.CreateRepository<UserRepository>();

                unitOfWork.Database.ExecuteSqlCommand("DELETE FROM Users");

                repository.Save(_testUser);
                unitOfWork.Flush();
            }
        }

        [TestMethod]
        public void Clear_Test()
        {
            using (var unitOfWork = new TestUnitOfWork())
            {
                var repository = unitOfWork.CreateRepository<UserRepository>();

                _testUser.Name = "段光宇";
                repository.Update(_testUser);
                unitOfWork.Clear();
                unitOfWork.Flush();

                var count = unitOfWork
                    .Database
                    .SqlQuery<int>("SELECT COUNT(*) FROM Users WHERE Name = {0}", "段光宇")
                    .First();

                Assert.AreEqual(0, count);
            }
        }

        [TestCleanup]
        public void TestCleanup()
        {
            using (var unitOfWork = new TestUnitOfWork())
            {
                unitOfWork.Database.ExecuteSqlCommand("DELETE FROM Users");
            }
        }
    }
}
